package com.music.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.mapper.SingleSongMapper;
import com.music.pojo.SingleSong;
import com.music.pojo.SingleSongExample;
import com.music.pojo.UserInfo;
import com.music.service.SingleSongService;
import com.music.utils.ResultInfo;
import com.music.utils.SingleSongInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SingleSongServiceImpl implements SingleSongService {
    @Autowired
    private SingleSongMapper singleSongMapper;
    @Override
    public ResultInfo SingleSongQuery(String id) {
        ResultInfo resultInfo =new ResultInfo();
//        List<SingleSong> list = singleSongMapper.;
        return null;
    }

    @Override
    public PageInfo<SingleSong> findSingleSongsBySearch(String search,Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<SingleSong> singleSongsBySearch = singleSongMapper.findSingleSongsBySearch(search);
        PageInfo<SingleSong> pageInfo = new PageInfo<>(singleSongsBySearch);
        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public SingleSong findWithCommentsAndSingerByMid(String mid) {
        return singleSongMapper.findWithCommentsAndSingerByMid(mid);
    }

    @Override
    public Integer isFavorite(Integer mid, Integer uid) {
        return singleSongMapper.isFavorite(mid,uid);
    }

    @Override
    public Integer findSongsNumCount(Integer sid) {
        return singleSongMapper.findSongsCount(sid);
    }

    @Override
    public PageInfo<SingleSong> findSoaringSongs(Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<SingleSong> soaringSongs = singleSongMapper.findSoaringSongRank();
        PageInfo<SingleSong> pageInfo = new PageInfo<>(soaringSongs);
        return pageInfo;
    }

    @Override
    public PageInfo<SingleSong> findNewSongs(Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<SingleSong> newSongs = singleSongMapper.findNewSongRank();
        PageInfo<SingleSong> pageInfo = new PageInfo<>(newSongs);
        return pageInfo;
    }

    @Override
    public PageInfo<SingleSong> findHotSong(Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<SingleSong> hotSongs = singleSongMapper.findHotSongRank();
        PageInfo<SingleSong> pageInfo = new PageInfo<>(hotSongs);
        return pageInfo;
    }

    @Override
    public List<SingleSong> findUserFollowSingerSongs(Integer uid) {
        return singleSongMapper.findUserFollowSingerSongs(uid);
    }

    @Override
    public SingleSong findSingleSongById(Integer id) {
        return singleSongMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<Integer, Map<Integer, Integer>> findUser_singleSongInfo(List<UserInfo> userInfoList) {
        Map<Integer,Map<Integer,Integer>> user_singleSongInfoMap = new HashMap<>();
        for (UserInfo userInfo:userInfoList) {
            Map<Integer,Integer> singleSongInfoMap = new HashMap<>();
            List<SingleSongInfo> singleSongInfoList = singleSongMapper.findSingleSongInfo(userInfo.getUid());
            for (SingleSongInfo singleSongInfo:singleSongInfoList) {
                singleSongInfoMap.put(singleSongInfo.getMid(),singleSongInfo.getCount());
            }
            user_singleSongInfoMap.put(userInfo.getUid(),singleSongInfoMap);
        }
        return user_singleSongInfoMap;
    }
}
